<?php 
if(!empty($_COOKIE["chat_username"]))
{
	file_put_contents(dirname(__FILE__)."/chat/".$_COOKIE["chat_username"],"welcome");
	$new_members=file_get_contents(dirname(__FILE__)."/new_member");
	if(strlen($new_members)>0)
	{
		$new_members.=",".$_COOKIE["chat_username"];
	}
	else 
	{
		$new_members=$_COOKIE["chat_username"];
	}
	lock_write(dirname(__FILE__)."/new_member",$new_members,"w");
}
else 
{
	header("Location:chat_login.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="./fckeditor/fckeditor.js"></script>
<script language="javascript">
function Comet(url)
{
	this.url=url;
	this.error=true;
	this.timestamp=0;
	this.doRequest=function(word)
	{
		word=escape(word);
		$.ajax({
		  type: "POST",
		  url: this.url+"?ran="+Math.random(),
		  data:'msg='+word
		});
	};
}

 var comet=new Comet("backend.php");
var comet_connect=function()
{
	$.ajax({
  type: "POST",
  url: comet.url+"?ran="+Math.random(),
  cache: false,
  dataType:'json',
  data:'timestamp='+comet.timestamp,
  success: function(response)
  {
	  comet.error=false;
	  comet.timestamp=response.timestamp;
	  $("#results").prepend("<div>"+response.msg+"</div>");
	 
  },
  complete:function(response)
  {
	  if(comet.error)
	  {
		   setTimeout(function(){comet_connect() }, 5000); 
	  }
	  else
	  {
		
		  comet_connect();
		
	  }
  }
});
}
$(document).ready(function(){
 
  comet_connect();
  $("#member_2").css("display","none");
   var word = new FCKeditor( 'word' ) ;
word.Width="500px";
word.Height="200px";
word.BasePath = "./fckeditor/" ;
word.ToolbarSet="Basic";
word.ReplaceTextarea() ;
}); 

function sendMsg()
{
	var word=$("#username").val();
	var time=new Date();
	word+='&nbsp;'+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds()+"<br>";
	word+=getEditorHTMLContents("word");
	comet.doRequest(word);
	SetEditorContents("word","");
	$("#results").prepend("<div>"+word+"</div>");
}

function getEditorHTMLContents(EditorName) {
    var oEditor = FCKeditorAPI.GetInstance(EditorName);
    return(oEditor.GetXHTML(true));
}

function SetEditorContents(EditorName, ContentStr) {
    var oEditor = FCKeditorAPI.GetInstance(EditorName) ;
    oEditor.SetHTML(ContentStr) ;
}
</script>
<style>
body{
	margin-left:auto; 
	margin-right:auto; 
	width:500px;
}
#chat_members
{
	width:200px; 
	float:left;
}
#chat_members ul{
	list-style:none;
}
#chat_members li
{
	padding-top:10px; 
	text-indent:40px;
	height:40px;  
	background-image:url(../images/chat_header.gif); 
	background-position: top left; 
	background-repeat:no-repeat
}
</style>
</head>

<body>
<div style=" float:right; width:500px;">
<div id="results" style="height:100px;overflow-y:scroll;"></div>
<form action="" method="get" >
<input type="hidden" name="username" id="username" value="<?php echo $_COOKIE["chat_username"]; ?>" />
    <textarea name="word" id="word"></textarea>
    <input type="button" name="button" onclick="sendMsg()" value="Send" />
  </form>
</div>
</body>
</html>
<?php 
function lock_write($file,$contents,$mode="a+")
{
	$fp = fopen($file, $mode);

	if (flock($fp, LOCK_EX)) 
	{ // 进行排它型锁定
		fwrite($fp, $contents);
		flock($fp, LOCK_UN); // 释放锁定
	}
	 else 
	 {
		//echo "Couldn't lock the file !";
	}

	fclose($fp);
}
?>